<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <h1>欢迎来到叩丁狼积分商城</h1>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" width="23px" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="login-btn cart" v-if="this.isLogined">
          <img src="../assets/img/cart.png" alt="" />
          购物车
          <p>{{ cartTotal }}</p>
        </li>
        <li class="login-btn" @click="changeIsShowLoginModal(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeiXinLoginAPI } from "@/request/api";
import Toast from "@/components/Toast.vue";

export default {
  data() {
    return {};
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  components: { Toast },
  // watch: {
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       // console.log("watch", newVal, oldVal);
  //       let token = localStorage.getItem("x-auth-token");
  //       // 判断是否有token切换登录/购物车按钮状态
  //       this.changeIsLogin(Boolean(token));
  //     },
  //   },
  // },
  methods: {
    ...mapMutations({
      // 方法名:"模块名/方法名"
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogin: "loginStatus/changeIsLogin",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  async created() {
    // 获取code(当前用户点击允许后,页面重新刷新,重新执行topbar中create钩子函数)
    let code = this.$route.query.code;
    // console.log("code", this.$route.query.code);
    // this.$router.onReady(() => {
    //   console.log("code111111", this.$route.query.code);
    // });

    if (code) {
      // 微信扫码登录
      // 找后台去微信换取token
      // console.log("发送微信请求");
      let res = await WeiXinLoginAPI({ code });
      console.log("微信登录结果", res);

      if (res.code == 0) {
        // 1.登陆成功
        // 1、提示用户登录成功
        this.asyncChangeIsShowToast({
          isShow: true,
          // 提示信息
          msg: "登录成功",
          // 提示类型
          type: "success",
        });
        // 2、保存token值
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 3、改变登录状态
        this.changeIsLogin(true);
        // 清除地址栏
        this.$route.query.code && this.$router.push(this.$route.path);
        // 扫码登录获取用户信息
        this.asyncChangeUserInfo();
      } else if (res.code == 400) {
        // 2.二维码失效
        // 2.1提示重新扫码
        this.asyncChangeIsShowToast({
          isShow: true,
          // 提示信息
          msg: "请重新扫码",
          // 提示类型
          type: "warning",
        });
        // 2.2自动打开登录模态框
        this.changeIsShowLoginModal(true);
      } else if (res.code == 407) {
        // 3.需要用户再进行一次手机号方式登录(后期让后台将微信和手机号关联)
        // 3.1提示用户需要进行一次手机号登录
        this.asyncChangeIsShowToast({
          isShow: true,
          // 提示信息
          msg: "请使用手机号再进行登录",
          // 提示类型
          type: "warning",
        });
        // 3.2自动打开登录模态框
        this.changeIsShowLoginModal(true);
        //  3.3存储uuid(一会登录使用)
        localStorage.setItem("uuid", res.uuid);
      }
    } else {
      // 判断是否有token
      let token = localStorage.getItem("x-auth-token");
      this.changeIsLogin(Boolean(token));

      if (token) {
        // 刷新不丢失用户信息
        this.asyncChangeUserInfo();
      } else {
        // 手动删除token初始化用户信息
        this.initUserInfo();
      }
    }
  },
};
</script>

<style lang="less" scoped>
.topbar {
  width: 100%;
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    text-align: center;
    .right {
      li {
        float: left;
        margin-left: 20px;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          background: #0a328e;
        }
        &.cart {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 0 10px;
          p {
            width: 20px;
            height: 20px;
            line-height: 20px;
            display: inline-block;
            background-color: #fd604d;
            border-radius: 50%;
            color: #fff;
          }
          img {
            width: 25px;
            height: 25px;
          }
        }
        img {
          vertical-align: middle;
          border-radius: 50%;
        }
      }
    }
  }
}
</style>